package il.ac.tau.cvws.scrabblesidekick.server.db.engine;


import il.ac.tau.cvws.scrabblesidekick.server.db.connection.MySQLConnection;
import il.ac.tau.cvws.scrabblesidekick.server.db.template.DBInsertTemplate;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBInsertionHandler 
{
	private DBInsertTemplate stringGenerator = new DBInsertTemplate();
	private MySQLConnection myConnect           = new MySQLConnection();
	
	
	/*
	 * LOGIC: the function inserts a letter from the alphabet of a given dictionary to the letter table in DB.
	 * INPUT: dictionaryId- identifier to dictionary, charcterId- identifier of letter, value- the value of this letter in scrabble game.
	 */
	public void insertLetter(int dictionaryId,int charcterID, char character, int value) throws SQLException
	{
		Connection connection = null;
		Statement statement   = null;

		try
		{
			connection = myConnect.getConnection();

			statement = connection.createStatement();
			statement.executeUpdate(stringGenerator.insertLetter(dictionaryId, charcterID, character, value));
		}
		catch(SQLException e)
		{
			throw e; 
		}
		finally
		{
			try
			{
				if (statement != null)  statement.close();
				if (connection != null) connection.close();
			}
			catch (SQLException e)
			{
				throw e; 
			}
		}
	}
	/*
	 * LOGIC: the function inserts a word to the word_n table in DB.
	 * INPUT: dictionaryId- identifier to dictionary,word- the word to insert into DB
	 */
	public void insertWordn(int dictionaryId,int[] word) throws SQLException
	{
		Connection connection = null;
		Statement statement   = null;

		try
		{
			connection = myConnect.getConnection();

			statement = connection.createStatement();
			statement.executeUpdate(stringGenerator.insertWord(dictionaryId, word));
		}
		catch(SQLException e)
		{
			throw e; 
		}
		finally
		{
			try
			{
				if (statement != null)  statement.close();
				if (connection != null) connection.close();
			}
			catch (SQLException e)
			{
				throw e; 
			}
		}
	}

	/*
	 * LOGIC: the function create a dictionary record in DB. 
	 * INPUT: name- name of language, bLeftRight- 0 means the reading order is not from left to right, otherwise- reading order is from left to right
	 * RETURN: the function returns the dictionaryId- identifier of the dictionary in db( auto incremented index of languages)
	 */
	public int insertDictionary(String name, int leftRight) throws SQLException
	{
		Connection connection = null;
		Statement statement   = null;
		int dictionaryID      = 0;
		
		try
		{
			connection = myConnect.getConnection();

			statement = connection.createStatement();

			statement.executeUpdate(stringGenerator.insertDictionary(name, leftRight), Statement.RETURN_GENERATED_KEYS);
			
			ResultSet rs = statement.getGeneratedKeys();

			rs.next();
			dictionaryID = rs.getInt(1);

		}
		catch(SQLException e)
		{
			throw e; 
		}
		finally
		{
			try
			{
				if (statement != null)  statement.close();
				if (connection != null)  connection.close();
			}
			catch (SQLException e)
			{
				throw e; 
			}
		}
		
		return dictionaryID;
	}
}
